@extends('layouts.application')
@section('content')
    <div class="admin-content">
        <div class="am-cf am-padding">
            <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">物流运费</strong> /
                <small>Express Manage</small>
            </div>
        </div>

        @include('layouts.shared._flash')

        <div class="am-g">
            <div class="am-u-sm-12 am-u-md-6">
                <div class="am-btn-toolbar">
                    <div class="am-btn-group am-btn-group-xs">
                        <a class="am-btn am-btn-default" href="{{route('shop.express.create')}}">
                            <span class="am-icon-plus"></span> 新增
                        </a>
                        <button type="button" class="delete_all am-btn am-btn-default"><span
                                    class="am-icon-trash-o"></span> 删除
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <div class="am-g">
            <div class="am-u-sm-12">
                <form class="am-form">
                    <table class="am-table am-table-striped am-table-hover table-main">
                        <thead>
                        <tr>
                            <th class="table-check"><input type="checkbox" class="check_all"></th>

                            <th>编号</th>
                            <th>物流名称</th>
                            <th>配送方式描述</th>
                            <th>运费 / 满额包邮</th>
                            <th>是否可用</th>
                            <th style="width:10%">排序</th>
                            <th class="table-set">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach($expresses as $express)
                            <tr data-id="{{$express->id}}">
                                <td><input type="checkbox" class="checked_id" value="{{$express->id}}"
                                           name="checked_id[]">
                                </td>
                                <td>{{ $express->id }}</td>
                                <td><a href="{{ $express->url }}" target="_blank">{{ $express->name }}</a></td>
                                <td>{{ $express->description }}</td>

                                <td>{{ $express->shipping_money }} / {{ $express->shipping_free }}</td>
                                <td class="am-hide-sm-only">
                                    {!! is_something($express,'is_enable') !!}
                                </td>

                                <td class="am-hide-sm-only">
                                    <input type="text" name="sort_order" class="sort_order am-input-sm"
                                           value="{{$express->sort_order}}">
                                </td>

                                <td>
                                    <div class="am-btn-toolbar">
                                        <div class="am-btn-group am-btn-group-xs">
                                            <a class="am-btn am-btn-default am-btn-xs am-text-secondary"
                                               href="{{route('shop.express.edit',$express->id)}}">
                                                <span class="am-icon-list-alt"></span> 编辑
                                            </a>
                                            <a class="delete_one am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only"
                                               href="#"
                                            >
                                                <span class="am-icon-trash-o"></span> 删除
                                            </a>
                                        </div>
                                    </div>
                                </td>
                            </tr>
                        @endforeach

                        </tbody>
                    </table>

                    <div class="am-cf">
                        <div class="am-fr">

                            {!! $expresses->links() !!}
                        </div>
                    </div>

                </form>
            </div>
        </div>

    </div>
@endsection

@section('js')
    <script type="text/javascript">
        $(function () {
            //单选删除
            $('.delete_one').click(function () {
                var id = $(this).parents('tr').data('id');
                var _this = $(this)
                $.ajax({
                    type: 'DELETE',
                    url: '/admin/shop/express/' + id,
                    data: {id: id},
                    success: function () {
                        _this.parents('tr').fadeOut(400);
                    }
                })
                return false;
            })
            //改变属性
            $('.change_attr').click(function () {
                var id = $(this).parents('tr').data('id');
                var attr = $(this).attr('data-attr')
                var _this = $(this)
                $.ajax({
                    type: "PATCH",
                    url: "{{route('shop.express.change_attr')}}",
                    data: {id: id, attr: attr},
                    success: function () {
                        _this.toggleClass('am-icon-check am-icon-close')
                    }

                })
                return false;
            })
            //排序
            $('.sort_order').change(function () {
                var id = $(this).parents('tr').data('id')
                var sort_order = $(this).val()

                $.ajax({
                    type: 'PATCH',
                    url: "{{route('shop.express.sort_order')}}",
                    data: {id: id, sort_order: sort_order},
                    success: function () {
                        location.reload();
                    }
                })
            })
            //全选反选
            $('.check_all').click(function () {
                $(':checkbox').prop('checked', this.checked);
            })

            $('.delete_all').click(function () {
                var length = $('.checked_id:checked').length
                if (length == 0) {
                    alert('您至少选择一条！')
                }
                var checked_id = $('.checked_id:checked').serialize()
//                console.log(checked_id);return false;
                $.ajax({
                    type: 'DELETE',
                    url: '{{route('shop.express.delete_all')}}',
                    data: checked_id,
                    success: function () {
                        $('.checked_id:checked').parents('tr').fadeOut(400)
                    }
                })
                return false;
            })
        })
        {{--$(function () {--}}

        {{--//排序--}}
        {{--$("input[name='sort_order']").change(function () {--}}
        {{--var data = {--}}
        {{--sort_order: $(this).val(),--}}
        {{--id: $(this).parents("tr").data('id')--}}
        {{--}--}}
        {{--$.ajax({--}}
        {{--type: "PATCH",--}}
        {{--url: "/shop/express/sort_order",--}}
        {{--data: data,--}}
        {{--dataType: 'json',--}}
        {{--success: function (data) {--}}
        {{--if (data.status == 0) {--}}
        {{--alert(data.msg);--}}
        {{--return false;--}}
        {{--}--}}
        {{--}--}}
        {{--});--}}
        {{--})--}}

        {{--//是否...--}}
        {{--$(".is_something").click(function () {--}}
        {{--var _this = $(this);--}}
        {{--var data = {--}}
        {{--id: _this.parents("tr").data('id'),--}}
        {{--attr: _this.data('attr')--}}
        {{--}--}}

        {{--$.ajax({--}}
        {{--type: "PATCH",--}}
        {{--url: "/shop/express/is_something",--}}
        {{--data: data,--}}
        {{--dataType: 'json',--}}
        {{--success: function (data) {--}}
        {{--if (data.status == 0) {--}}
        {{--alert(data.msg);--}}
        {{--return false;--}}
        {{--}--}}
        {{--_this.toggleClass('am-icon-close am-icon-check');--}}
        {{--}--}}
        {{--});--}}
        {{--});--}}
        {{--});--}}
    </script>
@endsection

